package com.zy.fee.roadpark.dao;

import android.content.Context;
import com.nanchen.compresshelper.CompressHelper;
import com.yanzhenjie.nohttp.FileBinary;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zy.fee.roadpark.config.Constants;
import com.zy.fee.roadpark.config.Urls;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.FileUtil;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.StringUtils;
import com.zygk.library.util.ToastUtil;
import java.io.File;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Wikison on 2019/9/19.
 */
public class FileLogic {
  public static File getFile(Context mContext, String path) {
    File newFile = null;
    try {
      if (FileUtil.getFileSize(path) == 0) {
        return null;
      } else {
        if (FileUtil.getFileSize(path) < 1000 * 1024) {
          newFile = new File(path);
        } else {
          newFile = CompressHelper.getDefault(mContext).compressToFile(new File(path));
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return newFile;
  }

  /**
   * 车牌识别
   */
//  public static void GetCPSB(final Context mContext, String path,
//      final HttpRequest.HttpCallback callback) {
//    File newFile = getFile(mContext, path);
//
//    if (null == newFile) {
//      ToastUtil.showMessage(mContext, "图片错误");
//      return;
//    }
//
//    StringRequest request = new StringRequest(Urls.SCAN_URL + Urls.GetCPSB, RequestMethod.POST);
//    request.add("File", new FileBinary(newFile));
//
//    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
//      @Override public void onStart(int what) {
//        super.onStart(what);
//        callback.onStart();
//      }
//
//      @Override public void onSucceed(int what, Response<String> response) {
//        super.onSucceed(what, response);
//        try {
//          JSONObject jsonObject = new JSONObject(response.get());
//          JSONArray plates = jsonObject.getJSONArray("plates");
//          if (plates != null && plates.length() > 0) { // 说明成功
//            JSONObject plate = plates.getJSONObject(0);
//            String plateNum = plate.getString("txt");
//
//            callback.onSucceed(plateNum);
//          } else {
//            callback.onFailed();
//          }
//        } catch (JSONException e) {
//          callback.onFailed();
//        }
//      }
//
//      @Override public void onFailed(int what, Response<String> response) {
//        super.onFailed(what, response);
//        callback.onFailed();
//      }
//
//      @Override public void onFinish(int what) {
//        super.onFinish(what);
//        callback.onFinish();
//      }
//    };
//    CallServer.getInstance().request(0, request, listener);
//  }

  public static void GetCPSB(final Context mContext, String path,
                             final HttpRequest.HttpCallback callback) {
    File newFile = getFile(mContext, path);

    if (null == newFile) {
      ToastUtil.showMessage(mContext, "图片错误");
      return;
    }

    StringRequest request = new StringRequest(Constants.KS_API_CPSB, RequestMethod.POST);
    request.add("image_file", new FileBinary(newFile));
    request.add("api_key", Constants.KS_API_KEY);
    request.add("api_secret", Constants.KS_API_SECRET);

    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        try {
          JSONObject jsonObject = new JSONObject(response.get());
          JSONArray plates = jsonObject.getJSONArray("results");

          if (plates != null && plates.length() > 0) { // 说明成功
            JSONObject plate = plates.getJSONObject(0);
            String plateNum = plate.getString("license_plate_number");

            callback.onSucceed(plateNum);
          } else {
            String errMsg = jsonObject.getString("error_message");
            if(!StringUtils.isBlank(errMsg)) {
              ToastUtil.showMessage(mContext, errMsg);
            }else {
              callback.onFailed();
            }
          }
        } catch (JSONException e) {
          callback.onFailed();
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        //callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
